﻿using System;
using System.Web.Security;
using MonkeyShop.Admin.Biz;
using MonkeyShop.Web.Administration.Common;

namespace MonkeyShop.Web.Administration
{
    public partial class Logon : System.Web.UI.Page
    {
        private AdminUserBiz _adminUserBiz;
        private AdminUserBiz AdminUserManager
        {
            get { return _adminUserBiz ?? (_adminUserBiz = new AdminUserBiz()); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnLogonClick(object sender, EventArgs e)
        {
            if (ValidateUser(txtUserName.Text, txtPassword.Text))
            {
                if (!string.IsNullOrEmpty(Request["ReturnUrl"]))
                {
                    FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, chkPersistCookie.Checked);
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(txtUserName.Text, chkPersistCookie.Checked);
                    Response.Redirect("~/Administration/Default.aspx");
                }
            }
            else
            {
                lblErrorMessage.Text = "Đăng nhập thất bại";
            }
        }

        private bool ValidateUser(string userName, string password)
        {
            var passwordHash = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
            var employee = AdminUserManager.Logon(userName, passwordHash);
            if (employee != null)
            {
                AdminCommon.CurrrentEmployee = employee;
            }

            return employee != null;
        }
    }
}